function parseCookies(cookieString)
        cookieTable = {}
        if not cookieString then return cookieTable end
--        for k, v in string.gmatch(cookieString, "(%p+%a+)=(%w+)") do
        for k, v in string.gmatch(cookieString, "([%p%w]+)=([^;]*)") do
                cookieTable[k] = v
        end
        return cookieTable;
end

function create_guid(create_time, ip)
    --Cat bot 1 ky tu khoi create time, lay toi da 9 ky tu => index
    --chuyen ip tu dang address with dot => ip (in_addr_t) bang ham inet_network cua linux
    if (not create_time) or (not ip) then return nil end
    local index
    if string.len(create_time) >=10 then
        index = 9
    else
        index = string.len(create_time) - 1;
    end
    local ipl = util.c_inet_network(ip)
    if (ipl==-1) then
        return false
    end
    return string.sub(create_time, 1, index)..ipl;
end

function pushCookieHeader(name, value, expires, path, domain, is_secure)
        if not (name and value) then return nil end
        local cookie = name.."="..value
        if expires then
            local cookie_time = os.date("%a, %d-%b-%Y %H:%M:%S",expires)
            cookie = cookie.."; expires="..cookie_time.." GMT"
        end
        if path then
                cookie = cookie.."; path="..path
        end
        if domain then
                cookie = cookie.."; domain="..domain
        end

        if is_secure then
                cookie = cookie.."; secure"
        end
        reply.appendHeader(config.SET_COOKIE_HEADER,cookie)
end

-- redis_cmd; cmd_format, [optional] cmd_data
util.redis_cmd = function (biz_name, enable_log, func, cmd_format, cmd_data)
    local con = getRedisConnector(biz_name)
    if con then
        local r = func(con, cmd_format,cmd_data);
        if not r then
        -- redisContext should not be used again
            redis_connectors[biz_name] = nil
            if enable_log then
            	io.log("e","Redis Server:", string.format("Cannot send command %s ", cmd_format.." "..cmd_data.." "..biz_name))
        	end
        else
            return r
        end
    end
    -- if enable_log then
    --      log
end

function getRedisConnector(biz_name)
        if redis_connectors[biz_name] then
            return redis_connectors[biz_name];
        else
            return createRedisConnector(biz_name);
        end
end

function createRedisConnector(biz_name)
        local host = ConfigRedisConnectors[biz_name].host;
        local port = ConfigRedisConnectors[biz_name].port;
        if host == nil or port == nil then
        	io.log("e", "Redis config: host or port is nil", string.format("biz_name:%s ",biz_name))
        end
        local hp = host..":"..port
        local try = CountTry[hp]
        if try and try > config.MAX_TRY_REDIS then
            return nil
        end
        redis_connectors[biz_name] = luredis.new_connector(host, port, config.REDIS_CONNECT_TIMEOUT);
        if redis_connectors[biz_name] then
            if try then CountTry[hp] = nil end
            return redis_connectors[biz_name];
        else            
            if not try then try = 1 else try = try+1 end
            if try > config.MAX_TRY_REDIS then
                io.log("f","Redis Server:", string.format("unable to connect to %s after trying %d times", hp, try))
            else
                io.log("e","Redis Server:", string.format("unable to connect to %s after trying %d times", hp, try))
            end
            CountTry[hp] = try
            return nil;
        end
end

--by KhanhHH
function getIpvnService()
        if ipvn_service_ then
                return ipvn_service_;
        else
                return createIpvnService();
        end
end
function createIpvnService()
        local host = config.IPVN_SERVICE_HOST;
        local port = config.IPVN_SERVICE_PORT;
         local hp = host..":"..port
        local try = CountTry[hp]
        if try and try > config.MAX_TRY_IPVN then return nil end
        ipvn_service_ = ipvn_service:new(host,port);
        if ipvn_service_[biz_name] then
            if try then CountTry[hp] = nil end
            return ipvn_service_;
        else
            if not try then try = 1 else try = try+1 end
            if try > config.MAX_TRY_REDIS then
                io.log("f","IPVN Service:", string.format("unable to connect to %s after trying %d times", hp, try))
            else
                io.log("e","IPVN Service:", string.format("unable to connect to %s after trying %d times", hp, try))
            end
            CountTry[hp] = try
            return nil;
        end
end

function thrift_isIpvn(ip)
	local ipvn
	local ipvn_service_ = getIpvnService()
	if(ipvn_service_ ~= nil) then
		ipvn = ipvn_service_:is_ipvn(ip)
	end
	if not ipvn then
		ipvn = ""
	end
	return ipvn
end

-- mobile service
function getMobileService()
        if mobile_service_ then
                return mobile_service_;
        else
                return createMobileService();
        end
end
function createMobileService()
        local host = config.MOBILE_SERVICE_HOST;
        local port = config.MOBILE_SERVICE_PORT;
        local hp = host..":"..port
        local try = CountTry[hp]
        if try and try > config.MAX_TRY_MOBILE then return nil end
        mobile_service_ = mobile_service:new(host,port);
        if mobile_service_ then
            if try then CountTry[hp] = nil end
            return mobile_service_;
        else
            if not try then try = 1 else try = try+1 end
            if try > config.MAX_TRY_REDIS then
                io.log("f","Mobile Service:", string.format("unable to connect to %s after trying %d times", hp, try))
            else
                io.log("e","Mobile Service:", string.format("unable to connect to %s after trying %d times", hp, try))
            end
            CountTry[hp] = try
            return nil;
        end
end

--return "" if can't get osVersion
function parseUserAgent(userAgent)    
    local mbl_service_ = getMobileService()
    if(mbl_service_ ~= nil) then
        return mbl_service_:parse_user_agent(userAgent)
    else
        return nil
    end
end

--by Luong - 26-09
--if key is not exist, return ""
function getStringValue(table, key)
    local value = table[key]
    if value then
            return value
    else
            return ""
    end
end
